/**
 * 格式化人民币 10000 => 10,000.00
 * @param {*} s 金额  根据金额/倍数 分单位：s/100
 * @param {*} n 保留几位小数
 */
export function moneyFormat(s, n) {
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + '').replace(/[^\d/.-]/g, '')).toFixed(n) + '';
    let l = s.split('.')[0].split('').reverse();
    let r = s.split('.')[1];
    let t = '';
    for (let i = 0; i < l.length; i++) {
        let a = i + 1;
        t += l[i] + (a % 3 === 0 && a !== l.length ? ',' : '');
    };
    return t.split('').reverse().join('') + '.' + r;
}
/**
 * 还原人民币 10,000.00 => 10000
 * @param {*} s 金额  String
 */
export function rmoney(s) {
    return parseFloat(s.replace(/[^\d\.-]/g, ''));
}

/**
 * list 根据code 转化name
 * demo: codeFormat(statusList, params.row.status);
 * @param {*} tag  Object  遍历的数据 {code：'11', name: 'hello'}
 * @param {*} val  String Numbre 显示的name
 */
export function codeFormat(tag, val) {
    let _arr = [];
    if (tag instanceof Array) {
        _arr = tag;
    }
    if (val === undefined || val === null) {
        return;
    }
    _arr.forEach(item => {
        if (item.code === val) {
            val = item.name;
        }
    });
    return val;
}

export function unequal(arrA, arrB, count) {
    if (!arrB[count]) {
        return arrA;
    }
    let _arr = [];
    arrA.forEach(item => {
        if (item.docType !== arrB[count].docType) {
            _arr.push(item);
        }
    });
    count++;
    return unequal(_arr, arrB, count);
}
export function toThousands(num) {
    var num = (num || 0).toString(), re = /\d{3}$/, result = '';
    while (re.test(num)) {
        result = RegExp.lastMatch + result;
        if (num !== RegExp.lastMatch) {
            result = ',' + result;
            num = RegExp.leftContext;
        } else {
            num = '';
            break;
        }
    }
    if (num) { result = num + result; }
    return result;
}

export function convertTreeData(rows, checkedKey) {
    function exists(rows, parentId) {
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].id == parentId) return true;
        }
        return false;
    }

    var nodes = [];
    // get the top level nodes
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        if (!exists(rows, row.parentId)) {
            nodes.push({
                id: row.id,
                title: row.name,
                parentId: row.parentId,
                checked: row[checkedKey] ? true : false,
                expand: true
            });
        }
    }

    var toDo = [];
    for (var i = 0; i < nodes.length; i++) {
        toDo.push(nodes[i]);
    }
    while (toDo.length) {
        var node = toDo.shift();	// the parent node
        // get the children nodes
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row.parentId == node.id) {
                console.log(row[checkedKey]);
                var child = {
                    id: row.id,
                    title: row.name,
                    parentId: row.parentId,
                    checked: row[checkedKey] ? true : false,
                    expand: true
                };
                if (node.children) {
                    node.children.push(child);
                } else {
                    node.children = [child];
                }
                toDo.push(child);
            }
        }
    }
    return nodes;
}

/**
* array转树状结构
* @param {Array} list 数组
* @param {String} id id的字符串
* @param {String} pid 父id的字符串
* @param {String} children children的字符串
* @param {String} name name的字符串
* @param {String} checked checked的字符串
* @return {Array} 数组
*/
export function transTreeData(options) {
    let a = options.list || [];
    let id = options.id || 'id';
    let pid = options.pid || 'parentId';
    let children = options.children || 'children';
    let name = options.name || 'name';
    let checked = options.checked || 'roleResourceId';
    let description = options.description;
    let loading = options.loading == false ? false : true;

    if (a.length == 0) return [];
    let r = [], hash = {}, i = 0, j = 0, len = a.length;
    for (; i < len; i++) {
        hash[a[i][id]] = a[i];
    }
    for (; j < len; j++) {
        let aVal = a[j], hashVP = hash[aVal[pid]];
        aVal.title = aVal[name];
        if (description) {
            aVal[description] = aVal[description];
        }
        if (!loading) {
            aVal.loading = false;
            aVal[children] = [];
        }
        else {
            // aVal.expand = true;
            aVal.checked = aVal[checked] && parseInt(aVal[checked]) > 0 ? true : false;
        }
        if (hashVP) {
            !hashVP[children] && (hashVP[children] = []);
            hashVP[children].push(aVal);
        } else {
            r.push(aVal);
        }
    }
    return r;
}
/**
 * 树结构转数组
 * @param {Array} tree 树结构
 * @param {String} id id的字符串
 * @param {String} pid  父id的字符串
 * @param {String} children children的字符串
 * @param {String} name name的字符串
* @return {Array} 数组
 */
export function TreeToArray(options) {
    let tree = options.tree || [];
    let id = options.id || 'id';
    let pid = options.pid || 'parentId';
    let children = options.children || 'childs';
    let name = options.name || 'name';
    let description = options.description;
    if (tree.length == 0) return [];
    let arr = [];
    let hash = {};
    let i = 0;
    let len = tree.length;
    for (; i < len; i++) {
        let _item = tree[i];
        let _obj = {}
        _obj[id] = _item[id];
        _obj[name] = _item[name];
        _obj[pid] = 0;
        if (description) {
            _obj[description] = _item[description];
        }
        arr.push(_obj);
        childData(_item[id], _item[children]);
    }

    function childData(_id, child) {
        if (!child) return;
        for (var k = 0; k < child.length; k++) {
            let _item = child[k];
            if (_item[children]) {
                childData(_item[id], _item[children])
            }
            let _obj = {}
            _obj[id] = _item[id];
            _obj[name] = _item[name];
            _obj[pid] = _id;
            if (description) {
                _obj[description] = _item[description];
            }
            arr.push(_obj);
        }
    }
    return arr;
}
